import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;

import javax.swing.JPanel;




import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;


@SuppressWarnings("serial")
public class MapPanel extends JPanel implements MouseListener, KeyListener{

	private Tile[][] map;
	private NewGame game;
	private Boolean passLandBuy = true;
	
	/**
	 * Input map, setting a new map panel.
	 * */
	public MapPanel(Tile[][] m, NewGame g){
		game = g;
		setPreferredSize(new Dimension(900, 500));
		addKeyListener(this);
		addMouseListener(this);
		map = m;
		setVisible(true);
		validate();
		setFocusable(true);
		
		//requestFocusInWindow();
	}
	
	/**
	 * @return true if player want to pass buying land else return false
	 * */
	public Boolean PassLandBuy(){
		return passLandBuy;
	}
	
	/**
	 * Setting passLabBuy option is true
	 * */
	public void setPassLB(){
		passLandBuy = true;
	}
	
	/**
	 * Drawing each tile in the map.
	 * */
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		for(int i = 0; i < 5; i ++){
			for(int j = 0; j < 9; j++){
				Tile tile = map[i][j];
				tile.draw(g, 100, 100);
		    }
	    }
		
		if(game.getState() == NewGame.State.playerTurn){
			game.getCurrentPlayer().draw(g, 50, 50);
		}
	}
	
	

		/**
		 * Mouse click action listener.
		 * */
		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			setFocusable(true);
			//System.out.println("player loc " + game.getCurrentPlayer().getMapLocation());
			if (game.getState() == NewGame.State.landGrant || game.getState() == NewGame.State.landBuy){
				Point point = e.getPoint();
				int x = point.x / 100;
				int y = point.y / 100;
				Tile selected = map[y][x];
				if (!selected.getClass().equals(TownTile.class) && !selected.isOwned()){
					passLandBuy = false;
					game.getCurrentPlayer().buyTile(selected, game.getState());
					repaint();
					game.nextPlayer();
				}else if(selected.getClass().equals(TownTile.class)){
					game.nextPlayer();
				}
			}else if(game.getState() == NewGame.State.playerTurn){
				Point p = e.getPoint();
				int mx = p.x;
				int my = p.y;
				Point pp = game.getCurrentPlayer().getMapLocation();
				int px = pp.x;
				int py = pp.y;
				if(mx > px + 25){
					px += 5;
				}else if (mx < px + 25){
					px -= 5;
				}
				if(my > py + 25){
					py += 5;
				}else if(my < py + 25){
					py -= 5;
				}
				
                if(px + 50 >= 400 && px <= 500 && py + 50 >= 200 && py <= 300){
					System.out.println("enter town");
					game.playerEnterTown();
				}else if(px >= 0 && px + 50 <= 900 && py >= 0 && py + 50 <= 500){
					game.getCurrentPlayer().setMapLocation(new Point(px, py));
					repaint();
				}
			}
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	
	
	

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			
			if(game.getState() == NewGame.State.playerTurn){
				
				int event = e.getKeyCode();
				Player player = game.getCurrentPlayer();
				Point p = player.getMapLocation();
				int x = p.x;
				int y = p.y;
				if(event == KeyEvent.VK_DOWN){
					y += 10;
				}else if(event == KeyEvent.VK_UP){
					y -= 10;
				}else if(event == KeyEvent.VK_RIGHT){
					x += 10;
				}else if(event == KeyEvent.VK_LEFT){
					x -= 10;
				}
				
				if(x + 50 > 400 && x < 500 && y + 50 > 200 && y < 300){
					System.out.println("enter town");
					game.playerEnterTown();
				}else if(x >= 0 && x + 50 <= 900 && y >= 0 && y + 50 <= 500){
					game.getCurrentPlayer().setMapLocation(new Point(x, y));
					repaint();
				}
			}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	
}
